package controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import dao.AttentionMapper;
import pojo.Attention;
import pojo.User;
import service.AttentionServiceI;

@Controller
@RequestMapping("attention")
public class AttentionController {


	@Autowired
	private AttentionServiceI attentionServiceI;
	/**
	 * 
	 * @Title: attentionuser   
	 * @Description: 关注用户
	 * @param: @param request
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("attentionuser")
	@ResponseBody
	public String attentionuser(HttpServletRequest request) {
		Integer user2Id=Integer.valueOf(request.getParameter("userId")).intValue();
		User user=(User) request.getSession().getAttribute("user");
		if(user==null) {
			return "unlogin";
		}
		Integer user1Id=user.getUserId();
		Attention attention=attentionServiceI.FindAttentionByUser(user1Id, user2Id);//查看你是否关注了他
		Attention attention2=attentionServiceI.FindAttentionByUser(user2Id, user1Id);//查看他是否关注了你
		if(attention!=null) {
			if(attention.getFtol()==1) {//如果你先关注了他，如果你关注了他，返回已经关注
				return "alreadyattention";
			}
		}
		else if(attention2!=null) {
			if(attention2.getLtof()==1) {//如果他先关注的你，然后你也关注了他，返回已经关注
				return "alreadyattention";
			}
		}
		else {//如果你们没有互相关注，如果他关注了你而你没有关注他
			int k=attentionServiceI.FindAttentionOrNot(user2Id, user1Id);//查看有没有进行关注，有关注，进行更新
			if(k>0) {
				Integer ltof=1;
				int i=attentionServiceI.UpdateAttention(user2Id, user1Id,ltof);
				if (i>0) {
					return "success";
				}else
					return "false";
			}else {//没有关注激励，进行插入
				Attention attention1=new Attention();
				attention1.setUser1Id(user1Id);
				attention1.setUser2Id(user2Id);
				Integer ftol=1;
				attention1.setFtol(ftol);
				Integer ltof=0;
				attention1.setLtof(ltof);
				int i=attentionServiceI.insertAttention(attention1);
				if(i>0) {
					return "success";
				}else {
					return "false";
				}
			}	
		}
		return null;
	}
	
	@Autowired
	private AttentionMapper attentionMapper;
	/**
	 * 
	 * @Title: unattention   
	 * @Description: 取消关注
	 * @param: @param request
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("unattention")
	@ResponseBody
	public String unattention(HttpServletRequest request) {
		Integer user2Id=Integer.valueOf(request.getParameter("userId")).intValue();
		User user=(User) request.getSession().getAttribute("user");
		Integer user1Id=user.getUserId();
		Attention attention=attentionServiceI.FindAttentionByUser(user1Id, user2Id);//查看是否你先关注的他
		Attention attention2=attentionServiceI.FindAttentionByUser(user2Id, user1Id);//查看是否他先关注的你
		if(attention!=null) {//你先关注了他
			if(attention.getLtof()==1) {//他也关注了你
				Attention attention21=new Attention();
				attention21.setAttentionId(attention.getAttentionId());
				int ftol=0;
				attention21.setFtol(ftol);
				int i=attentionMapper.updateByPrimaryKeySelective(attention21);//进行更新
				if(i>0) {
					return "success";
				}else {
					return "false";
				}
			}else {//你没有关注他，直接删除
				int attentionId=attention.getAttentionId();
				int j=attentionMapper.deleteByPrimaryKey(attentionId);
				if(j>0) {
					return "success";
				}else {
					return "false";
				}
			}
		}else if(attention2!=null) {//他关注了你
			if(attention2.getLtof()==1) {//你也关注了他
				Attention attention21=new Attention();
				attention21.setAttentionId(attention2.getAttentionId());
				int ltof=0;
				attention21.setLtof(ltof);
				int i=attentionMapper.updateByPrimaryKeySelective(attention21);
				if(i>0) {
					return "success";
				}else {
					return "false";
				}
			}else {//你没有关注他
				int attentionId=attention2.getAttentionId();
				int j=attentionMapper.deleteByPrimaryKey(attentionId);
				if(j>0) {
					return "success";
				}else {
					return "false";
				}
			}
		}
		return "";
	}
}
